// 接口地址配置 
// const baseUrl = "http://192.168.0.188:8068";
const baseUrl = "http://43.139.121.71:1001";
uni.setStorageSync("baseUrl", baseUrl)
let lang
module.exports = (vm) => {
	uni.$u.http.setConfig((config) => {
		loadingText: '努力加载中~';
		loadingTime: 100;
		config.baseURL = baseUrl
		return config
	})
	// 请求拦截
	uni.$u.http.interceptors.request.use((config) => {
		config.data = config.data || ''
		lang = uni.getStorageSync('lang')
		const loading = lang == "zh-Hans" ? '加载中...' : "loading..."
		if (lang == 'zh-Hans') {
			lang = 'zh-cn'
		} else if (lang == "en") {
			lang = "en"
		} else if (lang == "ja") {
			lang = "jap"
		}
		config.url = config.url + '?lang=' + lang
		if (config.custom.auth == true) {
			let token = uni.getStorageSync("token")
			config.data = {
				...config.data,
				...{
					token: token
				}
			}
		};
		uni.showLoading({
			title: loading
		})
		return config
	}, config => {
		return Promise.reject(config)
	})

	// 响应拦截
	uni.$u.http.interceptors.response.use((res) => {
		// console.log(res.config.url == "/app/mall-shop/isHaveShopStatus")
		uni.hideLoading()
		if (res.data.code != 1) {
			uni.$u.toast(res.data.msg)
			if (res.data.code == -5) {
				setTimeout(() => {
					uni.navigateTo({
						url: "/pages/setting/realName"
					})
				}, 500)
			}
			if (res.data.code == 3) {
				setTimeout(() => {
					uni.navigateTo({
						url: "/pages/login/login"
					})
				}, 500)
			}
			return Promise.reject(res)
		}
		return res
	}, (res) => {
		// 对响应错误做点什么 （statusCode !== 200）
		uni.hideLoading()
		return Promise.reject(res)
	})
}